package com.sofar.mybatisplus.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sofar.mybatisplus.entity.TblUser;
import com.sofar.mybatisplus.entity.common.AjaxResult;
import com.sofar.mybatisplus.entity.common.Result;
import com.sofar.mybatisplus.service.TblUserService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * (TblUser)表控制层
 *
 * @author makejava
 * @since 2022-08-31 09:05:50
 */
@RestController
@RequestMapping("tblUser")
public class TblUserController {
    /**
     * 服务对象
     */
    @Resource
    private TblUserService tblUserService;

    /**
     * 分页查询
     * @param tblUser
     * @return
     */
    @PostMapping("/queryByPage")
    public AjaxResult queryByPage(@RequestBody TblUser tblUser) {
        IPage<TblUser> page = new Page<>(1,10);
        IPage<TblUser> query = this.tblUserService.queryByPage(tblUser, page);
        return Result.success(query);
    }

    /**
     * 新增
     * @param req
     * @return
     */
    @PostMapping("/add")
    public AjaxResult add(@RequestBody TblUser req){
        boolean save = tblUserService.save(req);
        return save ? Result.success() : Result.error();
    }

    /**
     * 新增
     * @param req
     * @return
     */
    @PostMapping("/addUser")
    public AjaxResult addUser(@RequestBody TblUser req){
        boolean save = tblUserService.addUser(req);
        return save ? Result.success() : Result.error();
    }

    /**
     * 通过id查询
     * @param id
     * @return
     */
    @GetMapping("getById/{id}")
    public AjaxResult getById(@PathVariable("id") Long id){
        TblUser byId = tblUserService.queryById(id);
        return Result.success(byId);
    }

    /**
     * 修改
     * @param req
     * @return
     */
    @PutMapping("edit")
    public AjaxResult edit(@RequestBody TblUser req){
        boolean update = tblUserService.updateById(req);
        return update ? Result.success() : Result.error();
    }


    /**
     * 修改
     * @param req
     * @return
     */
    @PutMapping("editUser")
    public AjaxResult editUser(@RequestBody TblUser req){
        boolean update = tblUserService.updateById(req);
        return update ? Result.success() : Result.error();
    }

}

